package com.atguigu.tingshu.order.service.impl;


import com.atguigu.tingshu.account.AccountFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.mapper.OrderDetailMapper;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.order.service.AccountPayBizService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AccountPayBizServiceImpl implements AccountPayBizService {


    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private AccountFeignClient accountFeignClient;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private UserFeignClient userFeignClient;


    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void handlerAccountPay(OrderInfo orderInfo) {
        //走到这说明付款方式是余额支付
        //远程调用账户服务扣减余额
        //封装扣减账户所需要的参数

        AccountDeductVo accountDeductVo = new AccountDeductVo();

        accountDeductVo.setUserId(orderInfo.getUserId());
        accountDeductVo.setOrderNo(orderInfo.getOrderNo());
        accountDeductVo.setAmount(orderInfo.getOrderAmount());
        accountDeductVo.setContent(orderInfo.getOrderTitle());

        Result result = accountFeignClient.checkAndDeduct(accountDeductVo);

        //判断远程调用的结果是否为200
        if (result.getCode() != 200){
            throw new GuiguException(result.getCode(),result.getMessage());
        }

        //扣减余额成功,则修改订单状态,该称已支付
        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_PAID);
        orderInfoMapper.updateById(orderInfo);

        //远程调用用户微服务完成虚拟物品发货

        //构建虚拟物品发货的对象
        UserPaidRecordVo userPaidRecordVo = new UserPaidRecordVo();
        userPaidRecordVo.setOrderNo(orderInfo.getOrderNo());
        userPaidRecordVo.setUserId(orderInfo.getUserId());
        userPaidRecordVo.setItemType(orderInfo.getItemType());
        //根据订单编号查询订单明细获取购买项目ID列表
        List<OrderDetail> orderDetails = orderDetailMapper.selectList(new LambdaQueryWrapper<OrderDetail>()
                .eq(OrderDetail::getOrderId, orderInfo.getId())
                .select(OrderDetail::getItemId)
        );

        List<Long> itemIdList = orderDetails.stream().map(list -> list.getItemId()).collect(Collectors.toList());

        userPaidRecordVo.setItemIdList(itemIdList);

        Result result1 = userFeignClient.savePaidRecord(userPaidRecordVo);

        if (result1.getCode() != 200){
            throw new GuiguException(result1.getCode(),result1.getMessage());
        }
    }
}
